* Rost / Schneider 2008: Civil War Risk Assessments
* Analyse-do-File fuer neu erstellte Daten

version 9.1
set more off
capture log close

clear

log using cwdatalog.txt, replace

use riskassessments_data.dta, clear

tsset gwno year, yearly

* Descriptive Statistics
sum warstns aigenlz p2l anoclinv2 flinstab mill minconl fllgdppclz lpoplz floil flmtnestz c_peaceyears if controlns == 1

* Correlations
corr warstns aigenlz p2l anoclinv2 flinstab mill minconl fllgdppclz lpoplz floil flmtnestz c_peaceyears if controlns == 1

***** Model 1a (1977-2007)

logit warstns aigenlz p2l anoclinv2 flinstab mill minconl fllgdppclz lpoplz floil flmtnestz c_peaceyears if controlns == 1, nolog

* predicted probabilities for civil war onset for significant independent variables

* civil war probability of 'average country'
dis exp(_b[_cons])/(1 + exp(_b[_cons]))

* civil war probability for country with PTS = 2
dis exp(_b[_cons] + _b[aigenlz]*1)/(1 + exp(_b[_cons] + _b[aigenlz]*1))

* civil war probability for country with PTS = 5
dis exp(_b[_cons] + _b[aigenlz]*4)/(1 + exp(_b[_cons] + _b[aigenlz]*4))

* civil war probability for country with Polity2 = +10
dis exp(_b[_cons] + _b[anoclinv2]*1 + _b[p2l]*10)/(1 + exp(_b[_cons] + _b[anoclinv2]*1 + _b[p2l]*10))

* civil war probability for country with Polity2 = -10
dis exp(_b[_cons] + _b[anoclinv2]*1 + _b[p2l]*(-10))/(1 + exp(_b[_cons] + _b[anoclinv2]*1 + _b[p2l]*(-10)))

* civil war probability for country with military regime
dis exp(_b[_cons] + _b[mill]*1)/(1 + exp(_b[_cons] + _b[mill]*1))

* civil war probability for NO anocracy
dis exp(_b[_cons] + _b[anoclinv2]*1)/(1 + exp(_b[_cons] + _b[anoclinv2]*1))

* civil war probability for country with minor internal armed conflict
dis exp(_b[_cons] + _b[minconl]*1)/(1 + exp(_b[_cons] + _b[minconl]*1))

* civil war probability for country with level of economic development one standard deviation above mean
sum fllgdppclz if e(sample), d
dis exp(_b[_cons] + _b[fllgdppclz]*(r(mean)+r(sd)))/(1 + exp(_b[_cons] + _b[fllgdppclz]*(r(mean)+r(sd))))

* civil war probability for country with maximum level of economic development
dis exp(_b[_cons] + _b[fllgdppclz]*r(max))/(1 + exp(_b[_cons] + _b[fllgdppclz]*r(max)))

* civil war probability for country with minimum level of economic development
dis exp(_b[_cons] + _b[fllgdppclz]*r(min))/(1 + exp(_b[_cons] + _b[fllgdppclz]*r(min)))
list country year fllgdppclz l_gdppc if fllgdppclz == r(min) | fllgdppclz == r(max)

* civil war probability for country with important oil exports
dis exp(_b[_cons] + _b[floil]*1)/(1 + exp(_b[_cons] + _b[floil]*1))

* civil war probability for weak country with indiscriminate state terror
dis exp(_b[_cons] + _b[aigenlz]*4 + _b[p2l]*5 + _b[mill]*1 + _b[minconl]*1 + _b[fllgdppclz]*r(min))/(1 + exp(_b[_cons] + _b[aigenlz]*4 + _b[p2l]*5 + _b[mill]*1 + _b[minconl]*1 + _b[fllgdppclz]*r(min)))

* civil war probability for strong country with no violations of physical integrity rights
dis exp(_b[_cons] + _b[anoclinv2]*1 + _b[p2l]*-10 + _b[fllgdppclz]*r(max))/(1 + exp(_b[_cons] + _b[anoclinv2]*1 + _b[p2l]*-10 + _b[fllgdppclz]*r(max)))

* preparatory steps for in-sample risk assessments
predict phat_1a if e(sample)
label var phat_1a "predicted probabilities from model 1a"

gen phat5_1a = 1-(1-phat_1a)^5
label var phat5_1a "war probability for next 5 years, 1-(1-phat_1a)^5"

tab war5 if e(sample) & year < 2003

sum war5 if e(sample) & year < 2003

gen colors5_1a = .
replace colors5_1a = 0 if phat5_1a <= r(mean)
replace colors5_1a = 1 if phat5_1a >  r(mean) & phat5_1a <= 2*r(mean)
replace colors5_1a = 2 if phat5_1a >  2*r(mean) & phat5_1a <= 4*r(mean)
replace colors5_1a = 3 if phat5_1a >  4*r(mean)
replace colors5_1a = . if phat5_1a == .
label var colors5_1a "color-coded warning from green to red, based on phat5_1a"

* in-sample 5-yr predictions

tab colors5_1a war5 if e(sample) & year < 2003, row

***** Model 1b (1977-2007): only variables from Model 1a that have p-values < .1 (without p2l, flinstab, lpoplz, floil, flmtnestz, c_peaceyears)

logit warstns aigenlz anoclinv2 mill minconl fllgdppclz if controlns == 1, nolog

* preparatory steps for in-sample risk assessments
predict phat_1b if e(sample)
label var phat_1b "predicted probabilities from model 1b"

gen phat5_1b = 1-(1-phat_1b)^5
label var phat5_1b "war probability for next 5 years, 1-(1-phat_1b)^5"

tab war5 if e(sample) & year < 2003

sum war5 if e(sample) & year < 2003

gen colors5_1b = .
replace colors5_1b = 0 if phat5_1b <= r(mean)
replace colors5_1b = 1 if phat5_1b >  r(mean) & phat5_1b <= 2*r(mean)
replace colors5_1b = 2 if phat5_1b >  2*r(mean) & phat5_1b <= 4*r(mean)
replace colors5_1b = 3 if phat5_1b >  4*r(mean)
replace colors5_1b = . if phat5_1b == .
label var colors5_1b "color-coded warning from green to red, based on phat5_1b"

* in-sample 5-yr predictions

tab colors5_1b war5 if e(sample) & year < 2003, row

***** Footnote:

***** Model 1c (1977-2007): Model 1a, but fllgdppclz is replaced by a dummy that indicates missing value for gdppc data

logit warstns aigenlz p2l anoclinv2 flinstab mill minconl l_gdppc_miss lpoplz floil flmtnestz c_peaceyears if controlns == 1, nolog

***** Model 1d (1977-2007): Model 1a, but p2l, anoclinv2 and flinstab are replaced by a dummy that indicates missing value for polity data 

logit warstns aigenlz l_polity_miss mill minconl fllgdppclz lpoplz floil flmtnestz c_peaceyears if controlns == 1, nolog

***** Model 2a: restricted model 1977 - 2002, out-of-sample predictions 2003 - 2007

logit warstns aigenlz p2l anoclinv2 flinstab mill minconl fllgdppclz lpoplz floil flmtnestz c_peaceyears if controlns == 1 & year < 2003, nolog

* preparatory steps for out-of-sample risk assessments, 2003 - 2007
predict phat_2a if e(sample)
label var phat_2a "predicted probabilities from model 2a"
gen phat5_2a = 1-(1-phat_2a)^5
label var phat5_2a "war probability for next 5 years, 1-(1-phat_2a)^5, from model 2a"

tab war5 if e(sample)

sum war5 if e(sample)

gen colors02_2a = .
replace colors02_2a = 0 if phat5_2a <= r(mean)
replace colors02_2a = 1 if phat5_2a >  r(mean) & phat5_2a <= 2*r(mean)
replace colors02_2a = 2 if phat5_2a >  2*r(mean) & phat5_2a <= 4*r(mean)
replace colors02_2a = 3 if phat5_2a >  4*r(mean)
replace colors02_2a = . if phat5_2a == .
label var colors02_2a "color-coded warning from green to red, based on phat5_2a"

* out-of-sample risk assessments, 2003 - 2007, based on Model 2a
tab colors02_2a war5 if year == 2002, row
* list of countries with war5 == 1
list country year f1.warstns f2.warstns f3.warstns f4.warstns f5.warstns colors02_2a phat5_2a if e(sample) & year == 2002 & war5 == 1

* predictions, out of sample 2003-2007: P(onset) > .5 

logit warstns aigenlz p2l anoclinv2 flinstab mill minconl fllgdppclz lpoplz floil flmtnestz c_peaceyears if controlns == 1 & year < 2003, nolog

predict prob_onset_2a if controlns == 1 & year >= 2003 & year <= 2007

sum prob_onset_2a if year >= 2003 & year <= 2007, d

gen prob_onset_2a_class = .
replace prob_onset_2a_class = 0 if prob_onset_2a < .5
replace prob_onset_2a_class = 1 if prob_onset_2a >= .5 & prob_onset_2a != .
tab prob_onset_2a_class if year >= 2003 & year <= 2007, miss

list country year prob_onset_2a warstns if year >= 2003 & year <= 2007 & prob_onset_2a >= .5 & prob_onset_2a <= 1

list country year prob_onset_2a warstns if year >= 2003 & year <= 2007 & warstns == 1

tab warstns prob_onset_2a_class if year >= 2003 & year <= 2007, row

***** Model 2b: only variables from Model 3a that have p-values < .1 (without flinstab, lpoplz, floil, flmtnestz, c_peaceyears)

logit warstns aigenlz p2l anoclinv2 mill minconl fllgdppclz if controlns == 1 & year < 2003, nolog

* preparatory steps for out-of-sample risk assessments, 2003 - 2007
predict phat_2b if e(sample)
label var phat_2b "predicted probabilities from model 2b"
gen phat5_2b = 1-(1-phat_2b)^5
label var phat5_2b "war probability for next 5 years, 1-(1-phat_2b)^5, from model 2b"

tab war5 if e(sample)

sum war5 if e(sample)

gen colors02_2b = .
replace colors02_2b = 0 if phat5_2b <= r(mean)
replace colors02_2b = 1 if phat5_2b >  r(mean) & phat5_2b <= 2*r(mean)
replace colors02_2b = 2 if phat5_2b >  2*r(mean) & phat5_2b <= 4*r(mean)
replace colors02_2b = 3 if phat5_2b >  4*r(mean)
replace colors02_2b = . if phat5_2b == .
label var colors02_2b "color-coded warning from green to red, based on phat5_2b"

* out-of-sample risk assessments, 2003 - 2007, based on Model 2b
tab colors02_2b war5 if year == 2002, row
* list of countries with war5 == 1
list country year f1.warstns f2.warstns f3.warstns f4.warstns f5.warstns colors02_2b phat5_2b if e(sample) & year == 2002 & war5 == 1

* predictions, out of sample 2003-2007: P(onset) > .5 

logit warstns aigenlz p2l anoclinv2 mill minconl fllgdppclz if controlns == 1 & year < 2003, nolog

predict prob_onset_2b if controlns == 1 & year >= 2003 & year <= 2007

sum prob_onset_2b if year >= 2003 & year <= 2007, d

gen prob_onset_2b_class = .
replace prob_onset_2b_class = 0 if prob_onset_2b < .5
replace prob_onset_2b_class = 1 if prob_onset_2b >= .5 & prob_onset_2b != .
tab prob_onset_2b_class if year >= 2003 & year <= 2007, miss

list country year prob_onset_2b warstns if year >= 2003 & year <= 2007 & prob_onset_2b >= .5 & prob_onset_2b <= 1

list country year prob_onset_2b warstns if year >= 2003 & year <= 2007 & warstns == 1

tab warstns prob_onset_2b_class if year >= 2003 & year <= 2007, row

***** Model 2c: restricted model 1977 - 1997, out-of-sample predictions 1998 - 2002

logit warstns aigenlz p2l anoclinv2 flinstab mill minconl fllgdppclz lpoplz floil flmtnestz c_peaceyears if controlns == 1 & year < 1998, nolog

* preparatory steps for out-of-sample risk assessments, 1998 - 2002
predict phat_2c if e(sample)
label var phat_2c "predicted probabilities from model 2c"
gen phat5_2c = 1-(1-phat_2c)^5
label var phat5_2c "war probability for next 5 years, 1-(1-phat_2c)^5, from model 2c"

tab war5 if e(sample)

sum war5 if e(sample)

gen colors97_2c = .
replace colors97_2c = 0 if phat5_2c <= r(mean)
replace colors97_2c = 1 if phat5_2c >  r(mean) & phat5_2c <= 2*r(mean)
replace colors97_2c = 2 if phat5_2c >  2*r(mean) & phat5_2c <= 4*r(mean)
replace colors97_2c = 3 if phat5_2c >  4*r(mean)
replace colors97_2c = . if phat5_2c == .
label var colors97_2c "color-coded warning from green to red, based on phat5_2c"

* out-of-sample risk assessments, 1995 - 1999, based on Model 2c
tab colors97_2c war5 if year == 1997, row
* list of countries with war5 == 1
list country year f1.warstns f2.warstns f3.warstns f4.warstns f5.warstns colors97_2c phat5_2c if e(sample) & year == 1997 & war5 == 1

* predictions, out of sample 1998-2007: P(onset) > .5 

logit warstns aigenlz p2l anoclinv2 flinstab mill minconl fllgdppclz lpoplz floil flmtnestz c_peaceyears if controlns == 1 & year < 1998, nolog

predict prob_onset_2c9807 if controlns == 1 & year >= 1998 & year <= 2007

sum prob_onset_2c9807 if year >= 1998 & year <= 2007, d

gen prob_onset_2c9807_class = .
replace prob_onset_2c9807_class = 0 if prob_onset_2c9807 < .5
replace prob_onset_2c9807_class = 1 if prob_onset_2c9807 >= .5 & prob_onset_2c9807 != .
tab prob_onset_2c9807_class if year >= 1998 & year <= 2007, miss

list country year prob_onset_2c9807 warstns if year >= 1998 & year <= 2007 & prob_onset_2c9807 >= .5 & prob_onset_2c9807 <= 1

list country year prob_onset_2c9807 warstns if year >= 1998 & year <= 2007 & warstns == 1

tab warstns prob_onset_2c9807_class if year >= 1998 & year <= 2007, row

***** Model 2d: only variables from Model 2c that have p-values < .1 (without flinstab, lpoplz, floil, flmtnestz, c_peaceyears )
 
logit warstns aigenlz p2l anoclinv2 mill minconl fllgdppclz if controlns == 1 & year < 1998, nolog

* preparatory steps for out-of-sample risk assessments, 1998 - 2002
predict phat_2d if e(sample)
label var phat_2d "predicted probabilities from model 2d"
gen phat5_2d = 1-(1-phat_2d)^5
label var phat5_2d "war probability for next 5 years, 1-(1-phat_2d)^5, from model 2d"

tab war5 if e(sample)

sum war5 if e(sample)

gen colors97_2d = .
replace colors97_2d = 0 if phat5_2d <= r(mean)
replace colors97_2d = 1 if phat5_2d >  r(mean) & phat5_2d <= 2*r(mean)
replace colors97_2d = 2 if phat5_2d >  2*r(mean) & phat5_2d <= 4*r(mean)
replace colors97_2d = 3 if phat5_2d >  4*r(mean)
replace colors97_2d = . if phat5_2d == .
label var colors97_2d "color-coded warning from green to red, based on phat5_2d"

* out-of-sample risk assessments, 1998 - 2002, based on Model 2d
tab colors97_2d war5 if year == 1997, row
* list of countries with war5 == 1
list country year f1.warstns f2.warstns f3.warstns f4.warstns f5.warstns colors97_2d phat5_2d if e(sample) & year == 1997 & war5 == 1

* predictions, out of sample 1998-2007: P(onset) > .5 

logit warstns aigenlz p2l anoclinv2 mill minconl fllgdppclz if controlns == 1 & year < 1998, nolog

predict prob_onset_2d9807 if controlns == 1 & year >= 1998 & year <= 2007

sum prob_onset_2d9807 if year >= 1998 & year <= 2007, d

gen prob_onset_2d9807_class = .
replace prob_onset_2d9807_class = 0 if prob_onset_2d9807 < .5
replace prob_onset_2d9807_class = 1 if prob_onset_2d9807 >= .5 & prob_onset_2d9807 != .
tab prob_onset_2d9807_class if year >= 1998 & year <= 2007, miss

list country year prob_onset_2d9807 warstns if year >= 1998 & year <= 2007 & prob_onset_2d9807 >= .5 & prob_onset_2d9807 <= 1

list country year prob_onset_2d9807 warstns if year >= 1998 & year <= 2007 & warstns == 1

tab warstns prob_onset_2d9807_class if year >= 1998 & year <= 2007, row

***** Model 2e: restricted model 1977 - 1992, out-of-sample predictions 1993 - 1997

logit warstns aigenlz p2l anoclinv2 flinstab mill minconl fllgdppclz lpoplz floil flmtnestz c_peaceyears if controlns == 1 & year < 1993, nolog

* preparatory steps for out-of-sample risk assessments, 1993 - 1997
predict phat_2e if e(sample)
label var phat_2e "predicted probabilities from model 2e"
gen phat5_2e = 1-(1-phat_2e)^5
label var phat5_2e "war probability for next 5 years, 1-(1-phat_2e)^5, from model 2e"

tab war5 if e(sample)

sum war5 if e(sample)

gen colors92_2e = .
replace colors92_2e = 0 if phat5_2e <= r(mean)
replace colors92_2e = 1 if phat5_2e >  r(mean) & phat5_2e <= 2*r(mean)
replace colors92_2e = 2 if phat5_2e >  2*r(mean) & phat5_2e <= 4*r(mean)
replace colors92_2e = 3 if phat5_2e >  4*r(mean)
replace colors92_2e = . if phat5_2e == .
label var colors92_2e "color-coded warning from green to red, based on phat5_2e"

* out-of-sample risk assessments, 1993 - 1997, based on Model 2e
tab colors92_2e war5 if year == 1992, row
* list of countries with war5 == 1
list country year f1.warstns f2.warstns f3.warstns f4.warstns f5.warstns colors92_2e phat5_2e if e(sample) & year == 1992 & war5 == 1

* predictions, out of sample 1993-2007: P(onset) > .5 

logit warstns aigenlz p2l anoclinv2 flinstab mill minconl fllgdppclz lpoplz floil flmtnestz c_peaceyears if controlns == 1 & year < 1993, nolog

predict prob_onset_2e9307 if controlns == 1 & year >= 1993 & year <= 2007

sum prob_onset_2e9307 if year >= 1993 & year <= 2007, d

gen prob_onset_2e9307_class = .
replace prob_onset_2e9307_class = 0 if prob_onset_2e9307 < .5
replace prob_onset_2e9307_class = 1 if prob_onset_2e9307 >= .5 & prob_onset_2e9307 != .
tab prob_onset_2e9307_class if year >= 1993 & year <= 2007, miss

list country year prob_onset_2e9307 warstns if year >= 1993 & year <= 2007 & prob_onset_2e9307 >= .5 & prob_onset_2e9307 <= 1

list country year prob_onset_2e9307 warstns if year >= 1993 & year <= 2007 & warstns == 1

tab warstns prob_onset_2e9307_class if year >= 1993 & year <= 2007, row

***** Model 2f: only variables from Model 2e that have p-values < .1 (without flinstab, lpoplz, floil, flmtnestz)
 
logit warstns aigenlz p2l anoclinv2 mill minconl fllgdppclz c_peaceyears if controlns == 1 & year < 1993, nolog

* preparatory steps for out-of-sample risk assessments, 1993 - 1997
predict phat_2f if e(sample)
label var phat_2f "predicted probabilities from model 2f"
gen phat5_2f = 1-(1-phat_2f)^5
label var phat5_2f "war probability for next 5 years, 1-(1-phat_2f)^5, from model 2f"

tab war5 if e(sample)

sum war5 if e(sample)

gen colors92_2f = .
replace colors92_2f = 0 if phat5_2f <= r(mean)
replace colors92_2f = 1 if phat5_2f >  r(mean) & phat5_2f <= 2*r(mean)
replace colors92_2f = 2 if phat5_2f >  2*r(mean) & phat5_2f <= 4*r(mean)
replace colors92_2f = 3 if phat5_2f >  4*r(mean)
replace colors92_2f = . if phat5_2f == .
label var colors92_2f "color-coded warning from green to red, based on phat5_2f"

* out-of-sample risk assessments, 1993 - 1997, based on Model 2f
tab colors92_2f war5 if year == 1992, row
* list of countries with war5 == 1

* predictions, out of sample 1993-2007: P(onset) > .5 

logit warstns aigenlz p2l anoclinv2 mill minconl fllgdppclz c_peaceyears if controlns == 1 & year < 1993, nolog

predict prob_onset_2f9307 if controlns == 1 & year >= 1993 & year <= 2007

sum prob_onset_2f9307 if year >= 1993 & year <= 2007, d

gen prob_onset_2f9307_class = .
replace prob_onset_2f9307_class = 0 if prob_onset_2f9307 < .5
replace prob_onset_2f9307_class = 1 if prob_onset_2f9307 >= .5 & prob_onset_2f9307 != .
tab prob_onset_2f9307_class if year >= 1993 & year <= 2007, miss

list country year prob_onset_2f9307 warstns if year >= 1993 & year <= 2007 & prob_onset_2f9307 >= .5 & prob_onset_2f9307 <= 1

list country year prob_onset_2f9307 warstns if year >= 1993 & year <= 2007 & warstns == 1

tab warstns prob_onset_2f9307_class if year >= 1993 & year <= 2007, row

***** Model 3 = Model 1a: 1977 - 2007, risk assessments for 2008 - 2012

logit warstns aigenlz p2l anoclinv2 flinstab mill minconl fllgdppclz lpoplz floil flmtnestz c_peaceyears if controlns == 1, nolog

* preparatory steps for risk assessments, 2008 - 2012
predict phat_3 if year == 2007
label var phat_3 "predicted probabilities based on 2007 data, model 3"

gen phat5_3 = 1-(1-phat_3)^5
label var phat5_3 "war probability for 2008 - 2012, 1-(1-phat_3)^5"

tab war5 if e(sample)

sum war5 if e(sample)

gen colors07_3 = .
replace colors07_3 = 0 if phat5_3 <= r(mean)
replace colors07_3 = 1 if phat5_3 >  r(mean) & phat5_3 <= 2*r(mean)
replace colors07_3 = 2 if phat5_3 >  2*r(mean) & phat5_3 <= 4*r(mean)
replace colors07_3 = 3 if phat5_3 >  4*r(mean)
replace colors07_3 = . if phat5_3 == .
label var colors07_3 "color-coded warning for 2008 - 2012, based on phat5_3"

* risk assessments, 2008 - 2012
tab colors07_3 if year == 2007

* country list based on these risk assessments
list colors07_3 country if year == 2007 & colors07_3 == 0
list colors07_3 country if year == 2007 & colors07_3 == 1 
list colors07_3 country if year == 2007 & colors07_3 == 2 
list colors07_3 country if year == 2007 & colors07_3 == 3 
list colors07_3 country if year == 2007 & colors07_3 == . 

log close

exit